package com.zlj.accountant.app.weiget;

import com.zlj.accountant.R;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.SimpleAdapter.ViewBinder;

public class DrawerView extends LinearLayout{

	private boolean isCanOpen = true;

	private boolean isOpen;

	private View root;

	private ViewGroup vTop, vBottom;

	private ViewBinder mBinder;

	public DrawerView(Context context, ViewGroup parent,int topResource){
		super(context);
		LayoutInflater factory = LayoutInflater.from(context);
		root = factory.inflate(R.layout.lyt_widget_drawerview, parent);
		initWidget(topResource);
		regListener();
	}

	private void regListener(){
		vTop.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View v){
				if(isCanOpen){
					vBottom.setVisibility(isOpen ? GONE : VISIBLE);
					isOpen = isOpen ? false : true;
				}
			}
		});
	}

	private void initWidget(int topResource){
		vBottom = (ViewGroup)root.findViewById(R.id.v_bottom);
		vTop = (ViewGroup)root.findViewById(R.id.v_top);
		LayoutInflater factory = LayoutInflater.from(getContext());
		factory.inflate(topResource, vTop);
	}
	
	public void setTopData(Object[] form,int[] to){
		for(int i = 0; i < to.length; i++){
	        int j = to[i];
	        String value = form[i] == null ? "" : form[i].toString();
 	        View v = vTop.findViewById(j);
			if(!mBinder.setViewValue(vTop, form[i], value)){
				EditText t = (EditText)v;
				t.setText(value);
			}	        
        }
	}

	public boolean isCanOpen(){
		return isCanOpen;
	}

	public boolean isOpen(){
		return isOpen;
	}

	public void setOpen(boolean isOpen){
		this.isOpen = isOpen;
	}

	public ViewGroup getvTop(){
		return vTop;
	}

	public ViewGroup getvBottom(){
		return vBottom;
	}

}
